package org.whuims.pois.action;

import com.google.gson.Gson;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.whuims.pois.dao.ClusterDao;
import org.whuims.pois.dao.DataDao;
import org.whuims.pois.model.Cluster;
import org.whuims.pois.model.DataC;
import org.whuims.poms.bean.Story;
import org.whuims.poms.bean.Timeline;
import org.whuims.poms.bean.TimelineResult;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 坤 on 2014/5/22 0022.
 */
public class TimelineAction extends DispatchAction {
    private DataDao dataDao = new DataDao();
    private ClusterDao clusterDao = new ClusterDao();

    public ActionForward showTimeline(ActionMapping mapping, ActionForm form,
                                      HttpServletRequest request, HttpServletResponse response) {

        String clusterID = request.getParameter("clusterID");

        String sourceData = timeline(clusterID);

        request.setAttribute("data", sourceData);
        System.out.println("跳转成功！");
        return mapping.findForward("success");

    }

    public List<DataC> getDataList(Long clusterID) {
        List<DataC> listForDataC = new ArrayList<DataC>();
        Cluster cluster = (Cluster) clusterDao.get(clusterID);
        String simID = cluster.getSimilarid();
        String[] strings = null;
        if (simID.length() != 0) {
            strings = simID.split(",");
            System.out.println(strings.length);
            for (int i = 0; i < strings.length; i++) {
//                System.out.println("第" + i + "个：" + strings[i]);
                listForDataC.add(dataDao.get(Long.parseLong(strings[i])));
            }
        }
        return listForDataC;
    }

    /**
     * 热度图
     * @param clusterID
     * @return
     */
    private String timeline(String clusterID) {
        List<Story> data = new ArrayList<Story>();
        if (clusterID == null) {
            return "";
//            clusterID = "1400574469710104";
        }
        System.out.println("clusterID:" + clusterID);
        List<DataC> list = new TimelineAction().getDataList(Long.parseLong(clusterID));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy,MM,dd");
        System.out.println("开始遍历！:" + list.size());
        for (int i = 0; i < list.size(); i++) {
            DataC dataC = list.get(i);
            Story story = new Story();
            System.out.println(dataC.getId());
            story.setId(String.valueOf(dataC.getId()));
            String soureTitle = dataC.getSitename() + ":" + (dataC.getTitlecontent().length() != 0 ? dataC.getTitlecontent() : "不存在标题！");
            String title = "<a href=\"" + dataC.getTitlelink() + "\" target=\"_blank\">" + soureTitle + "</a>";
            story.setHeadline(title);
            System.out.println("url:" + dataC.getTitlelink());
            String url = dataC.getTitlelink().length() != 0 ? dataC.getTitlelink() : "#";

            story.setStartDate(sdf.format(dataC.getNewstime()));
            story.setText(dataC.getAbstext());
            data.add(story);
        }
        String today = sdf.format(System.currentTimeMillis());
        System.out.println("今天是：" + today);
        Timeline timeline = new Timeline("舆情轨迹", "追随舆情轨迹，查找事件根源！",
                today, data);
        TimelineResult timelineData = new TimelineResult(timeline);
        String sourceData = new Gson().toJson(timelineData);
        System.out.println(sourceData);
        return sourceData;
    }
}

 /*   public static void main(String[] args){
        List<Story> data = new ArrayList<Story>();
        String clusterID = "1400574469709924";
        System.out.println("clusterID:"+clusterID);
        List<DataC> list = new TimelineAction().getDataList(Long.parseLong(clusterID));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy,MM,dd");
        System.out.println("开始遍历！");
        for (int i = 0; i < list.size(); i++) {
            DataC dataC = list.get(i);
            Story story = new Story();
            System.out.println(dataC.getId());
*//*            story.setId(String.valueOf(dataC.getId()));*//*
            story.setHeadline(dataC.getSitename() + ":" + (dataC.getTitlecontent().length() != 0 ? dataC.getTitlecontent() : "不存在标题！"));
*//*            story.setUrl(dataC.getTitlelink().length() != 0 ? dataC.getTitlelink() : "#");*//*
            story.setStartDate(sdf.format(dataC.getNewstime()));
            story.setText("好好学习！");
            data.add(story);
        }

        Timeline timeline = new Timeline("新闻轨迹", "追随新闻轨迹，查找溯源！",
                "2013,10,29", data);
        String sourceData=new Gson().toJson(timeline);
        System.out.println(sourceData);
    }

}
*/